Maximum Subarray Sum
提出
code: python
import math
import os
import random
import re
import sys
from collections import Counter
#
# Complete the 'maximumSum' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
# 1. LONG_INTEGER_ARRAY a
# 2. LONG_INTEGER m
#
def maximumSum(a, m):
# Write your code here
a_mod = list(map(lambda x: x%m, a))
c = Counter(a_mod)
print(c)
if __name__ == '__main__':
q = int(input().strip())
for q_itr in range(q):
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0) m = int(first_multiple_input1) a = list(map(int, input().rstrip().split()))
result = maximumSum(a, m)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
from bisect import bisect,insort
T = int(input())
for _ in range(T):
cumulative_sums = []
sum_so_far = 0
max_sum = 0
for i in range(N):
sum_so_far = (sum_so_far + ari) % M pos = bisect(cumulative_sums, sum_so_far)
d = 0 if pos == i else cumulative_sumspos max_sum = max(max_sum, (sum_so_far + M - d) % M)
insort(cumulative_sums, sum_so_far)
print(max_sum)
メモ
https://www.youtube.com/watch?v=mTBrkcBBR18
https://scrapbox.io/files/61d0f6ff82e5d2001f99baa4.png